package org.eiahe.hr.attendance.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.excel.utils.ExcelUtil;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.eiahe.hr.attendance.domain.bo.EmployeeAttendanceTypeBo;
import org.eiahe.hr.attendance.domain.vo.EmployeeAttendanceTypeVo;
import org.eiahe.hr.attendance.service.IEmployeeAttendanceTypeService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.List;

/**
 * 员工考勤类型管理控制器
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/attendance/employeeAttendanceType")
public class EmployeeAttendanceTypeController extends BaseController {

    private final IEmployeeAttendanceTypeService employeeAttendanceTypeService;

    /**
     * 查询员工考勤类型列表
     */
    @SaCheckPermission("attendance:employeeAttendanceType:list")
    @GetMapping("/list")
    public TableDataInfo<EmployeeAttendanceTypeVo> list(EmployeeAttendanceTypeBo bo, PageQuery pageQuery) {
        return employeeAttendanceTypeService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取员工考勤类型详细信息
     */
    @SaCheckPermission("attendance:employeeAttendanceType:query")
    @GetMapping("/{id}")
    public R<EmployeeAttendanceTypeVo> getInfo(@PathVariable Long id) {
        return R.ok(employeeAttendanceTypeService.queryById(id));
    }

    /**
     * 新增员工考勤类型
     */
    @SaCheckPermission("attendance:employeeAttendanceType:add")
    @Log(title = "新增员工考勤类型", linkType = "attendance", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EmployeeAttendanceTypeBo bo) {
        return toAjax(employeeAttendanceTypeService.insertByBo(bo));
    }

    /**
     * 修改员工考勤类型
     */
    @SaCheckPermission("attendance:employeeAttendanceType:edit")
    @Log(title = "修改员工考勤类型", linkType = "attendance", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EmployeeAttendanceTypeBo bo) {
        return toAjax(employeeAttendanceTypeService.updateByBo(bo));
    }

    /**
     * 删除员工考勤类型
     */
    @SaCheckPermission("attendance:employeeAttendanceType:remove")
    @Log(title = "删除员工考勤类型", linkType = "attendance", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        return toAjax(employeeAttendanceTypeService.deleteWithValidByIds(List.of(ids)));
    }

    /**
     * 批量分配员工考勤类型
     */
    @SaCheckPermission("attendance:employeeAttendanceType:batchAssign")
    @Log(title = "批量分配员工考勤类型", linkType = "attendance", businessType = BusinessType.INSERT)
    @PostMapping("/batchAssign")
    public R<Void> batchAssign(@Validated @RequestBody EmployeeAttendanceTypeBo bo) {
        return toAjax(employeeAttendanceTypeService.batchAssign(bo));
    }

    /**
     * 查询员工在指定日期的考勤类型
     */
    @SaCheckPermission("attendance:employeeAttendanceType:query")
    @GetMapping("/employee/{employeeId}")
    public R<EmployeeAttendanceTypeVo> getByEmployeeId(
            @PathVariable Long employeeId,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) {
        LocalDate queryDate = date != null ? date : LocalDate.now();
        return R.ok(employeeAttendanceTypeService.queryByEmployeeIdAndDate(employeeId, queryDate));
    }

    /**
     * 导出员工考勤类型列表
     */
    @SaCheckPermission("attendance:employeeAttendanceType:export")
    @Log(title = "导出员工考勤类型", linkType = "attendance", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EmployeeAttendanceTypeBo bo, HttpServletResponse response) {
        List<EmployeeAttendanceTypeVo> list = employeeAttendanceTypeService.queryList(bo);
        ExcelUtil.exportExcel(list, "员工考勤类型数据", EmployeeAttendanceTypeVo.class, response);
    }
}
